//
//  ForgetPwdViewController.m
//  AppleMarket
//
//  Created by yangy on 25/10/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "ForgetPwdViewController.h"
// view
#import "ForgetPasswordView.h"
// viewModel
#import "ForgetViewModel.h"
// controller
#import "InputCodeViewController.h"
// model
#import "ImageCodeMoel.h"

@interface ForgetPwdViewController ()
@property (nonatomic, strong) ForgetPasswordView *mainView;
@property (nonatomic, strong) ForgetViewModel *viewModel;
@end

@implementation ForgetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.mainView];
    [self bindViewMode];
}

- (void) bindViewMode
{
    @weakify(self);
    [self.viewModel.sendCodeClickSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        InputCodeViewController *codeVC = [InputCodeViewController inputCodeViewControllerWithCodeType:CodeTypeForgotLoginPwd];
        codeVC.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.85];
        codeVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        codeVC.completeHandler = ^(ImageCodeMoel *codeModel, NSString *inputCodeStr) {
            // 调用发送验证码接口发送验证码
            [self.viewModel.refershSMSCodeCommand execute:RACTuplePack(codeModel.ImgKey,inputCodeStr)];
        };
        [self presentViewController:codeVC animated:YES completion:nil];
    }];
    
    [self.viewModel.commitClickSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        InputCodeViewController *codeVC = [InputCodeViewController inputCodeViewControllerWithCodeType:CodeTypeForgotLoginPwd];
        codeVC.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.85];
        codeVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        codeVC.completeHandler = ^(ImageCodeMoel *codeModel, NSString *inputCodeStr) {
            // 调用发送验证码接口发送验证码
            [self.viewModel.commitCommand execute:RACTuplePack(codeModel.ImgKey,inputCodeStr)];
        };
        [self presentViewController:codeVC animated:YES completion:nil];
    }];
    
    [self.viewModel.successSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

- (ForgetPasswordView *)mainView
{
    if(!_mainView){
        _mainView = [[ForgetPasswordView alloc] initWithFrame:self.view.bounds viewModel:self.viewModel];
    }
    return _mainView;
}

- (ForgetViewModel *)viewModel
{
    if(!_viewModel){
        _viewModel = [[ForgetViewModel alloc] init];
    }
    return _viewModel;
}

@end
